//
//  BaseNavigationController.m
//  tesDemo02
//
//  Created by 拓客营 on 2022/3/21.
//

#import "BaseNavigationController.h"


@interface BaseNavigationController ()<UINavigationControllerDelegate>
@property (nonatomic ,assign) id target;
@end

@implementation BaseNavigationController

-(id)initWithRootViewController:(UIViewController *)rootViewController{
    self = [super initWithRootViewController:rootViewController];
    if (self) {
//        [self.navigationController.navigationBar navBarBackGroundColor:nil image:[UIImage imageNamed:@"icon_no_data"] isOpaque:NO];
        
        // 代码布局从0开始，导航栏会遮挡
        // xib 加载会留出安全区域
        self.navigationBar.translucent = YES;
        self.toolbar.translucent = YES;
        
        [self.navigationBar navBarTitleColor:kNavTitleColor font:[UIFont systemFontOfSize:17]];
        [self.navigationBar navBarBackGroundColor:kThemeColor image:nil isOpaque:NO];
        [self.navigationBar navBarBottomLineHidden:YES];
        
        //    CGRect frame = self.navigationController.navigationBar.frame;
        //    UIView *alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, kNavigationBarHeight)];
        //    alphaView.backgroundColor = [UIColor greenColor];
        //    alphaView.alpha = 1;
        ////    [self.na.view insertSubview:alphaView belowSubview:self.navigationController.navigationBar];
        //    self.navigationItem.titleView = alphaView;
        
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 获取系统自带滑动手势的target对象
    self.target = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (viewController == [self.viewControllers firstObject] ){
        self.interactivePopGestureRecognizer.delegate = self.target;
    }
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.viewControllers.count > 0) {
        
        // 设置导航条左边按钮的内容，把系统的返回按钮给覆盖，导航控制器的滑动返回功能就消失了
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"icon_nav_back"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
        button.bounds = CGRectMake(0, 0, 44, 44);
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationBarHidden=NO;
        self.interactivePopGestureRecognizer.delegate = nil;
        
        UIViewController *firstViewController = self.viewControllers[0];
        UIViewController *currentViewController = self.viewControllers.lastObject;
//        [super pushViewController:viewController animated:animated];
        
        if (currentViewController == firstViewController) {

            currentViewController.hidesBottomBarWhenPushed = YES;
            [super pushViewController:viewController animated:animated];
            currentViewController.hidesBottomBarWhenPushed = NO;
        }
        else {

            currentViewController.hidesBottomBarWhenPushed = YES;
            [super pushViewController:viewController animated:animated];
        }
    }
    else { // tabbar 加载的时候调用
        [super pushViewController:viewController animated:animated];
    }
    
}

-(void)backButtonClick{
    [self popViewControllerAnimated:YES];
}

@end
